package com.hnld.transform.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class FileTransferUtil {

    public static void fileIndexTransfer(File file, String targetPath, Map<String, String> map, Map<String, String> upMap, String targetRouteName) {
        List<String> sourcesContents = FileUtil.readLines(file, "UTF-8");
        // 用于存储修改后的内容
        //C178431102A->2+510->01>K000+045_111.675803_26.254105.jpg 改成 C178431102A->000+045->01>K000+045_111.675803_26.254105.jpg
        List<String> targetContents = sourcesContents.stream().map(content -> {
            String[] split = content.split(">");
            String replaced = split[3].split("_")[0].replaceAll("K", "").replaceAll("k", "");
            String firstName = targetRouteName.concat("-");
            if (!map.isEmpty()) {
                String string = map.get(split[3]);
                content = firstName.concat(">").concat(replaced).concat("->").concat(string).concat("->").concat(split[3]);
            } else {
                content = firstName.concat(">").concat(replaced).concat("->").concat(split[2]).concat("->").concat(split[3]);
            }
            return content;
        }).collect(Collectors.toList());
        FileUtil.writeLines(targetContents, targetPath + "\\" + file.getName(), "UTF-8", true);
        System.out.println("写入" + targetPath + "\\" + file.getName() + "成功");
    }

    public static String extractContent(String patternStr, String text) {
        // 正则表达式匹配圆括号内的内容
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(text);
        if (!matcher.find()) {
            throw new RuntimeException("文本" + text + "正则表达式匹配失败");
        }
        return matcher.group(1);
    }

    public static File getLBIFile(String LBIPath, String routeCode) {
        List<File> LBIFiles = FileUtil.loopFiles(LBIPath);
        String substring = routeCode.substring(0, routeCode.length() - 1);
        List<File> fileList = LBIFiles.stream().filter(f -> f.getName().startsWith(substring)).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(fileList)) {
            //throw new RuntimeException("未找到路线"+routeCode+"的LBI数据");
            System.out.println("未找到路线" + routeCode + "的LBI数据");
            return null;
        }
        return fileList.get(0);
    }

    public static String getNewFileName(String routeCode, String type, String time, String[] DRSplit) {
        String fileSuffix = DRSplit[3];
        String fileName = fileSuffix.substring(0, fileSuffix.lastIndexOf("."));
        String newFileName = routeCode + "-" + type + "-" + DRSplit[2] + "-" + fileName + "-" + time + ".txt";
        if (FileUtil.exist(newFileName)) {
            System.out.println("文件类型" + type + "文件" + newFileName + "已存在,将在时间上+1");
            long times = Long.parseLong(time) + 1;
            return getNewFileName(routeCode, type, Long.toString(times), DRSplit);
        }
        return newFileName;
    }

    public static void main(String[] args) {
        String str = "C178431102A";
        System.out.println(str.substring(0, str.length() - 1));

    }
}
